package com.dingwen.treasure.influxdb.service.impl;

import com.dingwen.treasure.influxdb.config.InfluxdbConfig;
import com.dingwen.treasure.influxdb.service.BaseInlfuxdbService;
import com.influxdb.client.QueryApi;
import com.influxdb.client.WriteApi;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.query.FluxTable;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基础inlfuxdb服务impl
 *
 * @author dingwen
 * @date 2023/01/09
 */
public abstract class AbstractBaseInlfuxdbServiceImpl<T> implements BaseInlfuxdbService<T> {

    @Resource
    private WriteApiBlocking writeApiBlocking;

    @Resource
    private WriteApi writeApi;


    @Resource
    private QueryApi queryApi;

    @Resource
    private InfluxdbConfig influxdbConfig;


    @Override
    public void save(T t) {
        writeApiBlocking.writeMeasurement(
                getBucket(),
                influxdbConfig.getOrg(),
                WritePrecision.MS,
                t);
    }

    @Override
    public void asyncSave(T t) {
        writeApi.writeMeasurement(
                getBucket(),
                influxdbConfig.getOrg(),
                WritePrecision.MS,
                t);
    }

    @Override
    public List<FluxTable> querySerial(String fluxScript) {
        return queryApi.query(fluxScript);
    }
}
